<?php 
namespace Users\Logic;
class SmsLogic{
	private $sms;
	
	function __construct(){
		$this->sms = D('Sms');
	}
	
	//发送短信
	public function _send_sms($mobile,$code,$type){
		$content = "验证码为：".$code;
		//$mobileservice = new \Users\Service\MobileService('hhq110@126.com','haiqin880709');
		//$result = $mobileservice->sendSMS($mobile, $content, intval($mobile).microtime());
		//发送短信
		if(SMS_SEND($mobile,$content) == 1){
			$status = 1;
		}else{
			$status = 2;
		}
		$data = array(
				'mobile' => $mobile,
				'code'   => $code,
				'type'   => $type,
				'status' => $status,
				'hasuse' => 2,
				'token_time' => time()+60*30, //有效期30分钟
				'create_time' => Date('Y-m-d H:i:s'),
		);
		if($this->sms->add($data)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断短信验证码是否正确
	 * @param unknown $mobile
	 * @param unknown $code
	 * @param unknown $type
	 */
	public function _check_code($mobile,$code,$type){
		$map = array(
			'mobile' => $mobile,
			'type'   => $type,
			'status' => 1,
			'hasuse' => 2,
			'token_time' => array('EGT',time()),
		);
		$sms_info = $this->sms->where($map)->order('id desc')->limit(1)->find();
		if($sms_info['code'] === $code){
			$con['id'] = $sms_info['id'];
			$con['hasuse'] = 1;
			$this->sms->save($con);
			return true;
		}else{
			return false;
		}
	}
}
?>